<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height: 280px">
                    <div class="user-info">
                        <img :src="imageURL" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name"><span>Hi: </span>{{ name }}</div>
                            <div style="height: 20px">{{ role }}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        我的专业：
                        <span>{{ ms_major }}</span>
                    </div>
                    <div class="user-info-list">
                        我的班级：
                        <span>{{ ms_class }}</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height: 280px">
                    <div slot="header" class="clearfix">
                        <span class="user-info-list">个人详情</span>
                    </div>
                    <div class="user-info-list">
                        学号：<span class="user-info-list">{{ studentList.studentNumber }}</span>
                    </div>
                    <div class="user-info-list">
                        电话：<span class="user-info-list">{{ studentList.studentPhone }}</span>
                    </div>
                    <div class="user-info-list">
                        邮箱:<span class="user-info-list" style="margin-left: 80px">{{ studentList.studentEmail }}</span>
                    </div>
                    <div class="user-info-list">
                        小组：<span class="user-info-list">{{ teamName }}</span>
                    </div>
                    <div class="user-info-list">
                        课题：<span class="user-info-list">{{ projectName }}</span>
                    </div>
                    <div class="user-info-list">
                        老师：<span class="user-info-list">{{ teacherName }}</span>
                    </div>
                    <div class="user-info-list">
                        得分：<span class="user-info-list">{{ studentList.studentScore }}</span>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="16">
                <!-- <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-people grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">1234</div>
                                    <div>我的小组</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-notice grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">321</div>
                                    <div>我的消息</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-lx-goods grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">5000</div>
                                    <div>我的成绩</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row> -->
                <el-card shadow="hover" style="height: 575px">
                    <div slot="header" class="clearfix">
                        <span class="user-info-list">我的消息</span>
                        <el-button style="float: right; padding: 3px 0" type="text">课题中心</el-button>
                    </div>
                    <div>
                        <el-tabs v-model="message" @tab-click="readedMessage">
                            <el-tab-pane :label="`未读消息(${unread.length})`" name="first">
                                <el-table :data="unread" :show-header="false" style="width: 100%">
                                    <el-table-column>
                                        <template slot-scope="scope">
                                            <span class="message-title">{{ scope.row.message }}</span>
                                        </template>
                                    </el-table-column>
                                    <!-- <el-table-column prop="date" width="180"></el-table-column> -->
                                    <el-table-column width="120">
                                        <template slot-scope="scope">
                                            <el-button @click="handleRead(scope.row)">{{ messageDo }}</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                                <!-- <div class="handle-row">
                                    <el-button type="primary">全部标为已读</el-button>
                                </div> -->
                            </el-tab-pane>
                            <el-tab-pane :label="`已读消息(${read.length})`" name="second">
                                <template v-if="message === 'second'">
                                    <el-table :data="read" :show-header="false" style="width: 100%">
                                        <el-table-column>
                                            <template slot-scope="scope">
                                                <span class="message-title">{{ scope.row.message }}</span>
                                            </template>
                                        </el-table-column>
                                        <el-table-column prop="date" width="150"></el-table-column>
                                        <el-table-column width="120">
                                            <template slot-scope="scope">
                                                <el-button type="danger" @click="handleDel(scope.$index)">删除</el-button>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                    <!-- <div class="handle-row">
                                        <el-button type="danger">删除全部</el-button>
                                    </div> -->
                                </template>
                            </el-tab-pane>
                            <!-- <el-tab-pane :label="`回收站(${recycle.length})`" name="third">
                                    <template v-if="message === 'third'">
                                        <el-table :data="recycle" :show-header="false" style="width: 100%">
                                            <el-table-column>
                                                <template slot-scope="scope">
                                                    <span class="message-title">{{ scope.row.title }}</span>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="date" width="150"></el-table-column>
                                            <el-table-column width="120">
                                                <template slot-scope="scope">
                                                    <el-button @click="handleRestore(scope.$index)">还原</el-button>
                                                </template>
                                            </el-table-column>
                                        </el-table>
                                        <div class="handle-row">
                                            <el-button type="danger">清空回收站</el-button>
                                        </div>
                                    </template>
                                </el-tab-pane> -->
                        </el-tabs>
                    </div>
                </el-card>
                <!-- <el-card shadow="hover" style="height: 403px">
                    <div slot="header" class="clearfix">
                        <span>我的课题</span>
                        <el-button style="float: right; padding: 3px 0" type="text">课题中心</el-button>
                    </div>
                    <el-table :show-header="false" :data="todoList" style="width: 100%">
                        <el-table-column width="40">
                            <template slot-scope="scope">
                                <el-checkbox v-model="scope.row.status"></el-checkbox>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <div class="todo-item" :class="{ 'todo-item-del': scope.row.status }">{{ scope.row.title }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column width="60">
                            <template>
                                <i class="el-icon-edit"></i>
                                <i class="el-icon-delete"></i>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card> -->
            </el-col>
        </el-row>
        <!-- <el-row :gutter="20">
            <el-col :span="12">
                <el-card shadow="hover">
                    <schart ref="bar" class="schart" canvasId="bar" :options="options"></schart>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card shadow="hover">
                    <schart ref="line" class="schart" canvasId="line" :options="options2"></schart>
                </el-card>
            </el-col>
        </el-row> -->
        <!-- 审核弹出框 -->
        <el-dialog title="消息详情" :visible.sync="checkJoin" width="30%">
            <el-form :model="addteam" label-width="px">
                <el-form-item label="">
                    <span>{{ this.item }}</span>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button v-show="showFalse" type="danger" @click="refuseTeam()">拒绝</el-button>
                <el-button v-show="showPass" type="primary" @click="passJoin()">通过</el-button>
                <el-button v-show="hasRead" type="primary" @click="hasReadMessage()">已读</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import bus from '../common/bus';
export default {
    name: 'dashboard',
    data() {
        return {
            showPass: false,
            hasRead: true,
            showFalse: false,
            imageURL: '',
            item: '',
            checkJoin: false,
            message: 'first',
            showHeader: false,
            joinMessage: '',
            unread: [],
            read: [],
            studentID: '',
            hasTeam: '',
            teamName: '',
            projectName: '',
            teacherName: '',
            number: localStorage.getItem('ms_number'),
            nextTime: localStorage.getItem('ms_time'),
            role: localStorage.getItem('ms_role'),
            name: '',
            ms_class: '',
            ms_major: '',
            studentList: '',
            messageDo: '查看',
            hasProject: ''
        };
    },
    components: {
        Schart
    },
    computed: {
        role() {
            return this.role;
            //return this.name === 'admin' ? '超级管理员' : '普通用户';
        }
    },
    // created() {
    //     this.handleListener();
    //     this.changeDate();
    // },
    // activated() {
    //     this.handleListener();
    // },
    // deactivated() {
    //     window.removeEventListener('resize', this.renderChart);
    //     bus.$off('collapse', this.handleBus);
    // },
    created() {
        this.$http.get('http://localhost:9900/login/findStudent?number=' + this.number).then((res) => {
            console.log(res.data);
            this.studentList = res.data;
            this.studentID = res.data.studentId;
            //console.log(this.studentID);
            localStorage.setItem('ms_name', res.data.studentName), (this.name = res.data.studentName);
            this.ms_major = res.data.studentMajor.majorName;
            this.ms_class = res.data.studentClass.className;
            this.hasTeam = res.data.studentTeam;
            this.imageURL = res.data.studentImage;
            localStorage.setItem('imageURL', this.imageURL);
            localStorage.setItem('hasTeam', this.hasTeam);
            if (res.data.studentTeam == null) {
                this.teamName = '暂无';
                this.projectName = '暂无';
                this.teacherName = '暂无';
            } else {
                this.teamName = res.data.studentTeam.teamName;
                if (res.data.studentTeam.project == null) {
                    this.projectName = '暂无';
                    this.teacherName = '暂无';
                    localStorage.setItem('hasProject', false);
                } else {
                    this.projectName = res.data.studentTeam.project.projectName;
                    this.teacherName = res.data.studentTeam.project.teacher.teacherName;
                    localStorage.setItem('hasProject', true);
                }
            }
            this.$http.get('http://localhost:9900/message/findById?receiveID=' + this.number).then((msg) => {
                // localStorage.setItem("")
                console.log(msg);
                this.unread = msg.data;
            });
        });
    },
    methods: {
        refuseTeam(){
            this.checkJoin=false;
        },
        showStudentID() {
            console.log(this.studentID);
        },
        //未读消息
        handleRead(message) {
            console.log(message);
            this.joinMessage = message;
            this.checkJoin = true;
            console.log(this.joinMessage);
            if (message.message.indexOf('【退队审核】') != -1 || message.message.indexOf('【审核消息】') != -1) {
                console.log('true');
                this.showPass = true;
                this.showFalse = true;
                this.hasRead = false;
                this.item = message.message;
            } else {
                this.showPass = false;
                this.showFalse = false;
                this.hasRead = true;
                this.item = message.message;
                //this.checkJoin = false;
            }
        },
        handleDel(index) {
            const item = this.read.splice(index, 1);
            this.recycle = item.concat(this.recycle);
        },
        handleRestore(index) {
            const item = this.recycle.splice(index, 1);
            this.read = item.concat(this.read);
        },
        //已读消息
        hasReadMessage() {
            console.log(this.joinMessage);
            this.$http.get('http://localhost:9900/message/haveRead?id=' + this.joinMessage.id).then((msg) => {
                this.$message.success('修改消息状态成功！');
                this.checkJoin = false;
                location.reload();
            });
        },
        //已读的消息
        readedMessage() {
            if (this.message == 'second') {
                this.$http.get('http://localhost:9900/message/findReadedMessage?receiveID=' + this.number).then((msg) => {
                    this.read = msg.data;
                    console.log(msg.data);
                });
            }
        },
        // 通过审核
        passJoin() {
            if (this.joinMessage.message.indexOf('【退队审核】') != -1) {
                this.$http.post('http://localhost:9900/message/passExitTeam', this.joinMessage).then((res) => {
                    console.log(res.data);
                    this.checkJoin = false;
                    this.$message.success(`审核成功！ `);
                });
            }
            if (this.joinMessage.message.indexOf('【审核消息】') != -1) {
                this.$http.post('http://localhost:9900/message/passJoinTeam', this.joinMessage).then((msg) => {
                    this.checkJoin = false;
                    this.$message.success(`审核成功！ `);
                });
            }
        }
    },
    computed: {
        unreadNum() {
            return this.unread.length;
        }
    },
    changeDate() {
        const now = new Date().getTime();
        this.data.forEach((item, index) => {
            const date = new Date(now - (6 - index) * 86400000);
            item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
        });
    },
    // handleListener() {
    //     bus.$on('collapse', this.handleBus);
    //     // 调用renderChart方法对图表进行重新渲染
    //     window.addEventListener('resize', this.renderChart);
    // },
    // handleBus(msg) {
    //     setTimeout(() => {
    //         this.renderChart();
    //     }, 200);
    // },
    // renderChart() {
    //     this.$refs.bar.renderChart();
    //     this.$refs.line.renderChart();
    // }
    teamFormatter(row, column, cellValue) {
        if (cellValue.studentTeam == null) {
            return '暂无';
        } else {
            return cellValue.studentTeam.teamName;
        }
    }
};
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
.messageStyle {
    margin-top: 50px;
    margin-left: 50px;
}

.message-title {
    cursor: pointer;
}
.handle-row {
    margin-top: 30px;
}
</style>
